package com.hzcx.app.simplechat.ui.user.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hzcx.app.simplechat.MyApplication;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.ui.friend.bean.FriendLableListBean;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.view.flowlayout.FlowLayout;

public class LableFlowAdapter extends FlowLayout.ItemView<FriendLableListBean> {

    private OnItemOnClickListener listener;
    private String lastContent;

    @Override
    public void getCover(FriendLableListBean item, FlowLayout.ViewHolder holder, View inflate, int position) {
        TextView tvContent = holder.getView(R.id.tv_content);
        EditText etEdit = holder.getView(R.id.et_edit);
        tvContent.setText(item.getName());
        if (item.isChecked()) {
            tvContent.setBackgroundResource(R.drawable.draw_tv_lable_select);
            tvContent.setTextColor(MyApplication.getContext().getResources().getColor(R.color.color_2D7CFD));
        } else {
            tvContent.setBackgroundResource(R.drawable.draw_tv_lable_normal);
            tvContent.setTextColor(MyApplication.getContext().getResources().getColor(R.color.color_282F3E));
        }
        holder.setText(R.id.tv_content, item.getName());

        tvContent.setOnClickListener(view -> {
            if (listener != null) {
                listener.itemOnClick(position);
            }
        });

        // 键盘回车监听
        etEdit.setOnKeyListener((view, i, keyEvent) -> {
            if (KeyEvent.ACTION_DOWN == keyEvent.getAction()) {
                switch (i) {
                    case KeyEvent.KEYCODE_ENTER:
                        if (listener != null) {
                            listener.editEnter(etEdit.getText().toString(), position);
                            lastContent = "";
                        }
                        return true;
                }
            }
            return false;
        });

        etEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                lastContent = etEdit.getText().toString();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        if (EmptyUtils.isEmpty(item.getName())) {
            etEdit.setVisibility(View.VISIBLE);
            tvContent.setVisibility(View.GONE);
        } else {
            etEdit.setVisibility(View.GONE);
            tvContent.setVisibility(View.VISIBLE);
        }

    }

    public String getLastConent(){
        return lastContent;
    }

    public interface OnItemOnClickListener {
        void itemOnClick(int pos);

        void editEnter(String content, int pos);
    }

    public void setOnItemOnClickListener(OnItemOnClickListener listener) {
        this.listener = listener;
    }
}
